package benny.ApkposStore.db;

import benny.ApkposStore.model.DishNew;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class OrderTableDishs {
	/* table name */
	public static final String TABLENAME = "apkpos_orders_dishs";
	public static final String TPrintNAME = "apkpos_dishs_print";
	/* column names */
	static final public String CID = "_id"; // AUTO generate
	static final public String CTS = "cts"; // timestamp

	static final public String CORDERID = "orderid";
	static final public String DISH_ID = "dish_id";
	static final public String DNBR_VAR = "dnbr_var";
	static final public String DNAM_VAR = "dnam_var";
	static final public String DISH_NUM = "dish_num";
	static final public String DISH_PRICE = "dish_price";
	public static final String DISH_UNIT = "dish_unit";
	public static final String UNIT_ID = "unit_id";
	public static final String DISH_TIME = "dish_time";
	public static final String FREE_STATE = "free_state";
	public static final String ORDER_STATE = "order_state";
	public static final String ORDER_MODE = "order_mode";
	public static final String DCY_NAME = "dcy_name";
	public static final String CZY_NAME = "czy_name";
	public static final String DIC_RATE = "dic_rate";
	public static final String SVC_STATE = "svc_state";
	public static final String COOK_CONTENT = "cook_content";
	public static final String COOK_EXTRA = "cook_extra";
	public static final String ORIGINAL_PRICE = "original_price";
	public static final String LBXH = "lbxh";
	public static final String LBNAME = "lbname";
	public static final String PDET_VAR = "pdet_var";
	public static final String PDET_VAR_IP = "pdet_var_ip";
	public static final String IS_PRINT = "is_print";

	private Context mCtx;

	public static final String createTableSQL = "create table if not exists "
			+ TABLENAME + "(" + CID + " integer primary key autoincrement,"
			+ CTS + " int not null default 0," + CORDERID
			+ " varchar(40)  not null default 0," + DISH_ID
			+ " int not null default 0," + DNBR_VAR
			+ " int not null default 0," + DNAM_VAR
			+ "  varchar(50) not null ," + DISH_NUM
			+ " numeric(18,2) not null default 0," + DISH_PRICE
			+ " numeric(18,2) not null default 0," + DISH_UNIT
			+ " varchar(10) ," + UNIT_ID + " int ," + DISH_TIME
			+ " varchar(20) ," + FREE_STATE + " int ," + ORDER_STATE
			+ "  int ," + ORDER_MODE + " varchar(10) ," + DCY_NAME
			+ " varchar(15) ," + CZY_NAME + " varchar(15)," + DIC_RATE
			+ "  numeric(18,2) ," + SVC_STATE + " varchar(2) ," + COOK_CONTENT
			+ " varchar(200) ," + COOK_EXTRA + " numeric(18,2) ,"
			+ ORIGINAL_PRICE + " numeric(18,2)," + LBXH + " varchar(10),"
			+ LBNAME + " varchar(20)," + PDET_VAR + " varchar(10),"
			+ PDET_VAR_IP + " varchar(20)," + IS_PRINT + " int)";

	public static final String createPrintSQL = "create table if not exists "
			+ TPrintNAME + "(" + CID + " integer primary key autoincrement,"
			+ CTS + " int not null default 0," + CORDERID
			+ " varchar(40)  not null default 0," + DISH_ID
			+ " int not null default 0," + DNBR_VAR
			+ " int not null default 0," + DNAM_VAR
			+ "  varchar(50) not null ," + DISH_NUM
			+ " numeric(18,2) not null default 0," + DISH_PRICE
			+ " numeric(18,2) not null default 0," + DISH_UNIT
			+ " varchar(10) ," + UNIT_ID + " int ," + DISH_TIME
			+ " varchar(20) ," + FREE_STATE + " int ," + ORDER_STATE
			+ "  int ," + ORDER_MODE + " varchar(10) ," + DCY_NAME
			+ " varchar(15) ," + CZY_NAME + " varchar(15)," + DIC_RATE
			+ "  numeric(18,2) ," + SVC_STATE + " varchar(2) ," + COOK_CONTENT
			+ " varchar(200) ," + COOK_EXTRA + " numeric(18,2) ,"
			+ ORIGINAL_PRICE + " numeric(18,2)," + LBXH + " varchar(10),"
			+ LBNAME + " varchar(20)," + PDET_VAR + " varchar(10),"
			+ PDET_VAR_IP + " varchar(20)," + IS_PRINT + " int)";

	private static String tag = "apkpos_orders_dishs";

	public OrderTableDishs(Context ctx) {
		mCtx = ctx;
	}

	/**
	 * insert a row
	 * 
	 * @param bean
	 *            :
	 * @param context
	 *            :Application's context
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insert(DishNew bean) {

		ContentValues values = beanToContentValues(bean);
		values.remove(CID);

		try {
			SQLiteDatabase db = DBHelper.getDatabase(mCtx);
			long rowId = db.insertOrThrow(TABLENAME, null, values);
			Log.d(tag, "insert a row success");
			return rowId;
		} catch (SQLiteException e) {
			Log.e(tag, "insert " + TABLENAME + " error:" + e.getMessage());
		}

		return -1;
	}

	/**
	 * get a table cursor
	 * 
	 * @param ctx
	 *            :Application's context
	 * @return cursor
	 */
	public Cursor getCurSor(String selection, String selectionArgs[]) {
		String columns[] = { CID, CTS, CORDERID, DISH_ID, DNBR_VAR, DNAM_VAR,
				DISH_NUM, DISH_PRICE, DISH_UNIT, UNIT_ID, DISH_TIME,
				FREE_STATE, ORDER_STATE, ORDER_MODE, DCY_NAME, CZY_NAME,
				DIC_RATE, SVC_STATE, COOK_CONTENT, COOK_EXTRA, ORIGINAL_PRICE,
				LBXH, LBNAME, PDET_VAR };
		// String selection = CORDERID +"= ?";
		// String selectionArgs[] = {"100"};
		String groupBy = "";
		String having = "";
		String orderBy = "";
		String limit = "";

		Cursor cursor = null;
		try {
			SQLiteDatabase db = DBHelper.getDatabase(mCtx);
			cursor = db.query(TABLENAME, columns, selection, selectionArgs,
					groupBy, having, orderBy, limit);
			return cursor;
		} catch (SQLiteException e) {
			Log.e(tag, "query failed:" + e.getMessage());
		}
		return null;
	}

	/**
	 * update a service record
	 * 
	 * @param bean
	 */
	public boolean update(DishNew bean) {
		try {
			String whereClause = " _id= " + bean.getId();
			SQLiteDatabase db = DBHelper.getDatabase(mCtx);
			ContentValues values = beanToContentValues(bean);
			db.update(TABLENAME, values, whereClause, null);
			Log.d(tag, "update a row success");
		} catch (SQLiteException e) {
			Log.e(tag, "update " + TABLENAME + " error:" + e.getMessage());
			return false;
		}
		return true;
	}

	/**
	 * delete a row
	 * 
	 * @param rowId
	 * @param ctx
	 * @return
	 */
	public boolean delete(int rowId) {
		try {
			SQLiteDatabase db = DBHelper.getDatabase(mCtx);
			db.execSQL("delete from " + TABLENAME + " where _id = " + rowId);
			Log.d(tag, "delete a row success");
		} catch (SQLiteException e) {
			Log.e(tag, "delete failed:" + e.getMessage());
			return false;
		}
		return true;
	}

	/**
	 * 按订单号删除
	 * 
	 * @param orderid
	 * @return
	 */
	public boolean deleteByOrderid(String orderid) {
		try {
			SQLiteDatabase db = DBHelper.getDatabase(mCtx);
			db.execSQL("delete from " + TABLENAME + " where " + CORDERID
					+ " = " + orderid);
			Log.d(tag, "delete a row success");
		} catch (SQLiteException e) {
			Log.e(tag, "delete failed:" + e.getMessage());
			return false;
		}
		return true;
	}

	/**
	 * convert db cursor to bean structure
	 * 
	 * @param cursor
	 * @return Order
	 */
	public DishNew CurSorToGasBean(Cursor cursor) {
		DishNew bean = new DishNew();
		bean.setId(cursor.getInt(cursor.getColumnIndex(CID)));
		bean.setTs(cursor.getInt(cursor.getColumnIndex(CTS)));
		bean.setOrderid(cursor.getString(cursor.getColumnIndex(CORDERID)));
		bean.setDish_id(cursor.getInt(cursor.getColumnIndex(DISH_ID)));
		bean.setDnbr_var(cursor.getInt(cursor.getColumnIndex(DNBR_VAR)));
		bean.setDnam_var(cursor.getString(cursor.getColumnIndex(DNAM_VAR)));
		bean.setDish_num(cursor.getInt(cursor.getColumnIndex(DISH_NUM)));
		bean.setDish_price(cursor.getInt(cursor.getColumnIndex(DISH_PRICE)));
		bean.setDish_unit(cursor.getString(cursor.getColumnIndex(DISH_UNIT)));
		bean.setUnit_id(cursor.getInt(cursor.getColumnIndex(UNIT_ID)));
		bean.setDish_time(cursor.getString(cursor.getColumnIndex(DISH_TIME)));
		bean.setFree_state(cursor.getInt(cursor.getColumnIndex(FREE_STATE)));
		bean.setOrder_state(cursor.getInt(cursor.getColumnIndex(ORDER_STATE)));
		bean.setOrder_mode(cursor.getString(cursor.getColumnIndex(ORDER_MODE)));
		bean.setDcy_name(cursor.getString(cursor.getColumnIndex(DCY_NAME)));
		bean.setCzy_name(cursor.getString(cursor.getColumnIndex(CZY_NAME)));
		bean.setDic_rate(cursor.getInt(cursor.getColumnIndex(DIC_RATE)));
		bean.setSvc_state(cursor.getInt(cursor.getColumnIndex(SVC_STATE)));
		bean.setCook_content(cursor.getString(cursor
				.getColumnIndex(COOK_CONTENT)));
		bean.setCook_extra(cursor.getInt(cursor.getColumnIndex(COOK_EXTRA)));
		bean.setOriginal_price(cursor.getInt(cursor
				.getColumnIndex(ORIGINAL_PRICE)));
		bean.setLbxh(cursor.getInt(cursor.getColumnIndex(LBXH)));
		bean.setLbname(cursor.getString(cursor.getColumnIndex(LBNAME)));
		bean.setPdet_var(cursor.getString(cursor.getColumnIndex(PDET_VAR)));

		return bean;

	}

	private ContentValues beanToContentValues(DishNew bean) {
		ContentValues values = new ContentValues();

		values.put(CID, bean.getId());
		values.put(CTS, bean.getTs());
		values.put(CORDERID, bean.getOrderid());
		values.put(DISH_ID, bean.getDish_id());
		values.put(DNBR_VAR, bean.getDnbr_var());
		values.put(DNAM_VAR, bean.getDnam_var());
		values.put(DISH_NUM, bean.getDish_num());
		values.put(DISH_PRICE, bean.getDish_price());
		values.put(DISH_UNIT, bean.getDish_unit());
		values.put(UNIT_ID, bean.getUnit_id());
		values.put(DISH_TIME, bean.getDish_time());
		values.put(FREE_STATE, bean.getFree_state());
		values.put(ORDER_STATE, bean.getOrder_state());
		values.put(ORDER_MODE, bean.getOrder_mode());
		values.put(DCY_NAME, bean.getDcy_name());
		values.put(CZY_NAME, bean.getCzy_name());
		values.put(DIC_RATE, bean.getDic_rate());
		values.put(SVC_STATE, bean.getSvc_state());
		values.put(COOK_CONTENT, bean.getCook_content());
		values.put(COOK_EXTRA, bean.getCook_extra());
		values.put(ORIGINAL_PRICE, bean.getOriginal_price());
		values.put(LBXH, bean.getLbxh());
		values.put(LBNAME, bean.getLbname());
		values.put(PDET_VAR, bean.getPdet_var());

		return values;
	}

}
